import {browserHistory} from 'react-router';
import {Tool} from '../Tool';
import Util from '../utils/Util';
import {message} from "antd";

/* 加载动画 */
export const setSystemAnimating=(text,animating)=>{
    return {
        type: 'set_system_animating',
        text,
        animating
    }
};

export const setHomeLoadingMore=(isLoadingMore)=>{
    return {
        type: 'set_home_loadingMore',
        isLoadingMore
    }
};

export const SaveLoginUser=(user)=>{
    return {
        type:"loginSuccess",
        UserJson:user
    }
}

export const setCommonList=(tabName,result)=>{
    return {
        type:tabName,
        result
    }
}

export const openExpertFormModel=(obj)=>{
    return {
        type:"set_visible",
        visible:obj
    }
}

export const showCommonLoading=(obj)=>{
    return {
        type:"set_common_loading",
        loading:obj
    }
}

export const showCommonSearchLoading=(obj)=>{
    return {
        type:"set_common_search_loading",
        loading:obj
    }
}

export const showCommonlistLoading=(obj)=>{
    return {
        type:"set_common_list_loading",
        loading:obj
    }
}

/* 加载分页列表 */
export const loadCommonList=(tabName,key,page,number)=>{
    //false 加载完成    true正在加载
    return dispatch=>{
        //dispatch(setNotifyLoading(tabName,true));
        Tool.fetchGet(Util.getApi(key) + page + "/"+number, "", {}, 'json', 'basic',
            (res) => {
                if (!res.serror) {
                    dispatch(setCommonList(tabName,res.result));
                }
                setTimeout(function () {
                    //dispatch(setNotifyLoading(tabName,false));
                    dispatch(showCommonlistLoading(false));
                },500)
            }, (err) => {
                //dispatch(setNotifyLoading(tabName,false));
                dispatch(showCommonlistLoading(false));
                console.log(err);
            });
    }
}

/* 搜索专家 */
export const LoadSearchExpert=(tabName,name,org)=>{
    return dispatch=>{
        Tool.fetchGet(Util.getApi("SeExpert") + name + "/"+org, "", {}, 'json', 'basic',
            (res) => {
                if (!res.serror) {
                    var obj = {};
                    obj.content = res.result;
                    dispatch(setCommonList(tabName,obj));
                }else{
                    message.error(res.serror.desc)
                }
                setTimeout(function () {
                    dispatch(showCommonSearchLoading(false));
                },500)
            }, (err) => {
                console.log(err);
            });
    }
}
export const ExpertInfoModel=(uid)=>{
    return {
        type:"set_expert_info_uid",
        uid:uid
    }
}
/* 更新专家 */
export const UpdateExpertInfo=(values)=>{
    return {
        type:"set_expert_info_update",
        values:values
    }
}

/* 专家lab */
export const LoadExpertLab=(name)=>{
    return dispatch=>{
        Tool.fetchGet(Util.getApi("ExpertLab") + name, "", {}, 'json', 'basic',
            (res) => {
                if (!res.serror) {
                    dispatch(setCommonList("set_expert_lab",res.result));
                }
                setTimeout(function () {
                },500)
            }, (err) => {
                console.log(err);
            });
    }
}
/* 专家org */
export const LoadExpertOrg=(name)=>{
    return dispatch=>{
        Tool.fetchGet(Util.getApi("ExpertOrg") + name, "", {}, 'json', 'basic',
            (res) => {
                if (!res.serror) {
                    dispatch(setCommonList("set_expert_org",res.result));
                }
                setTimeout(function () {
                },500)
            }, (err) => {
                console.log(err);
            });
    }
}

/* 专家更新 */
export const LoadExpertUpdate=(method,values)=>{
    return dispatch=>{
        Tool.fetchCommon(method,Util.getApi("ExpertUpdate"),JSON.stringify(values), {}, 'json', 'basic',
            (res) => {
                if (!res.serror) {
                    if(res.result.code==200){
                        dispatch(openExpertFormModel(false));
                        /* 成功 */
                        if(method=="POST"){
                            /* 新加 */
                            message.success('专家增加成功');
                        }else{
                            /* update */
                            dispatch(UpdateExpertInfo(values));
                            message.success('更新成功');
                        }
                    }
                }else{
                    message.error(res.serror.desc);
                }
                setTimeout(function () {
                    dispatch(showCommonLoading(false));
                },500)
            }, (err) => {
                console.log(err);
            });
    }
}

